home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / editor / snmp_0_1.zip / snmp-0.1 / aka / snmp / MibManagerSerializer.java < prev    next >
Text File  |  1997-06-09  |  3KB  |  128 lines

  1. /*
  2. Snmp Library
  3. Copyright (C) 1997 Alex Kowalenko Associates Pty Ltd. All rights reserved.
  4.  
  5. $Id: MibManagerSerializer.java,v 1.2 1997/05/29 12:54:32 alex Exp $
  6.  
  7. This software maybe be free distributed, any any form, without fee, 
  8. but may not be modified in any way without express permission of 
  9. the directors of Alex Kowalenko Associates Pty Ltd. 
  10.  
  11. Alex Kowalenko Associates Pty Ltd makes no representations or
  12. warranties about the suitabililty of the software, not even the
  13. implied warranty of merchantability or fitness for any particular
  14. purpose.    
  15. */
  16.  
  17. package aka.snmp;
  18.  
  19. import java.util.*;
  20. import java.io.*;
  21.  
  22. /**
  23.  * Extends MibManagerPaser with methods to store and retrieve the
  24.  * managed object definitions as a Java serialized file.
  25.  * @see SnmpObject
  26.  * @version     $Id: MibManagerSerializer.java,v 1.2 1997/05/29 12:54:32 alex Exp $
  27.  * @author      Alex Kowalenko
  28.  */
  29.  
  30. public class MibManagerSerializer extends MibManagerParser {
  31.  
  32. /**
  33.  * Constructor
  34.  */
  35.  
  36.   public MibManagerSerializer() {
  37.       super();
  38.   };
  39.   
  40. /**
  41.  * Store the Managed Object definitions to a file.
  42.  * @see MibManager#SerializeFromFile
  43.  */
  44.  
  45.   public void SerializeToFile(String filename) 
  46.       throws FileNotFoundException, IOException {
  47.       FileOutputStream f = new FileOutputStream(filename);
  48.       ObjectOutput s = new ObjectOutputStream(f);
  49.       for(Enumeration e = _tableName.elements(); e.hasMoreElements(); ) {
  50.           s.writeObject((SnmpObject)e.nextElement());
  51.       };
  52.       s.flush();
  53.       s.close();
  54.   };
  55.  
  56. /**
  57.  * Read the Managed object defintion from a BER encoded file
  58.  * @see MibManagerSerializer#SerializeToFile
  59.  */
  60.  
  61.   public void SerializeFromFile(String filename)  
  62.       throws FileNotFoundException, IOException, SnmpMibException {
  63.       FileInputStream f = new FileInputStream(filename);
  64.       ObjectInput s = new ObjectInputStream(f);
  65.       try {
  66.           while(true) {
  67.           SnmpObject obj = (SnmpObject) s.readObject();
  68.           _tableName.put(obj.name(), obj);
  69.           _tableID.put(obj.id(), obj);
  70.           };
  71.       } catch(IOException e) {
  72.           // System.out.println(e.getMessage());
  73.           // finish
  74.       } catch(ClassNotFoundException e) {
  75.           throw new SnmpMibException("Serialization problem: " + 
  76.                      e.getMessage());
  77.       }
  78.       finally {
  79.           s.close();
  80.       };
  81.   };
  82.  
  83. /**
  84.  * Test harness
  85.  */
  86.     
  87.   public static void main(String args[]) {
  88.       PrintStream out = System.out;
  89.  
  90.       out.println("Creating SNMP MIB MANAGER");
  91.       MibManagerSerializer mibs = new MibManagerSerializer();
  92.  
  93.       out.println("Reading SNMP mib");
  94.       try {
  95.       mibs.addFromMibDefFile("mib1.txt");
  96.       } catch(FileNotFoundException e) {
  97.       out.println("File not found");
  98.       return;
  99.       } catch(Exception e) {
  100.       out.println("Exception");
  101.       e.printStackTrace();
  102.       mibs.dump();
  103.       };
  104.  
  105.       mibs.dump();
  106.  
  107.       try {
  108.       out.println("Write to serialization file");
  109.       mibs.SerializeToFile("mibs.ser");
  110.       } catch(Exception e) {
  111.       out.println("Excepton with SerializeFromFile : " + e.getMessage());
  112.       e.printStackTrace();
  113.       };
  114.  
  115.       MibManagerSerializer mib3 = new MibManagerSerializer();
  116.       try {
  117.       out.println("Read from serialization file");
  118.       mib3.SerializeFromFile("mibs.ser");
  119.       } catch(Exception e) {
  120.       out.println("Excepton with SerializeToFile : " + e.getMessage());
  121.       e.printStackTrace();
  122.       };
  123.       mib3.dump();
  124.  
  125.   };
  126.  
  127. };
  128.